//超级管理员路由
const express=require('express')
const admin=express.Router()
const axios=require('axios')

//分页查询管理员信息//queryForUserMessage
admin.get('/Admin/queryForAllAdmin',(req,res)=>{
    let {pageNow,pageSize}=req.query
    axios({
        method:'get',
        url:'/Sadmin/User/queryForAllAdmin',
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
            token:(req.session.token?req.session.token:'')
        },
        params:{
            pageNow:pageNow||1,
            pageSize:pageSize||6
        }
    }).then(value=>{
        res.status(200).json({
            key:value.data
        })
    }).catch(err=>{
        res.status(200).json({
            error:err
        })
    })
})


//根据id查询管理员信息
admin.get('/Admin/queryForOneAdmin',(req,res)=>{
    let {id}=req.query
    axios({
        method:'get',
        url:'/Sadmin/User/queryForOneAdmin',
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
            token:(req.session.token?req.session.token:'')
        },
        params:{
            id:id
        }
    }).then(value=>{
        res.status(200).json({
            key:value.data
        })
    }).catch(err=>{
        res.status(200).json({
            error:err
        })
    })
})

//搜索管理员
admin.get('/Admin/query',(req,res)=>{
    let {pageNow,pageSize,name}=req.query
    axios({
        method:'get',
        url:'/Sadmin/User/query',
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
            token:(req.session.token?req.session.token:'')
        },
        params:{
            pageNow:pageNow||1,
            pageSize:pageSize||6,
            name:name
        }
    }).then(value=>{
        res.status(200).json({
            key:value.data
        })
    }).catch(err=>{
        res.status(200).json({
            error:err
        })
    })
})


//更新用户等级
admin.put('/Admin/updateLevel',(req,res)=>{
    let {id,permission}=req.query
    axios({
        method:'put',
        url:'/Sadmin/User/updateLevel',
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
            token:(req.session.token?req.session.token:'')
        },
        params:{
            id:id,
            permission:permission
        }
    }).then(value=>{
        res.status(200).json({
            key:value.data
        })
    }).catch(err=>{
        res.status(200).json({
            error:err
        })
    })
})


//修改管理员信息
admin.get('/Admin/putForOne',(req,res)=>{
    let {id,account,album,area,email,fans,names,password,portrait,resume,sex,age,multipartFile}=req.query
    axios({
        method:'get',
        url:'/Sadmin/User/queryForOneAdmin',
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
            token:(req.session.token?req.session.token:'')
        },
        params:{
            id:id
        }
    }).then(value=>{
        let message=value.data.data
        let accountv=message.account//账号
        // console.log(account);
        let namev=message.name//昵称
        // console.log(name);
        let sexv=message.sex//性别
        // console.log(sex);
        let agev=message.age//年龄
        // console.log(age);
        let portraitv=message.portrait//头像
        // console.log(portrait);
        let areav=message.area//地区
        // console.log(area);
        let emailv=message.email//邮箱
        // console.log(email);
        let resumev=message.resume//简介
        // console.log(resume);
        let fansv=message.fans//粉丝
        // console.log(fans);
        let albumv=message.album//专辑
        // console.log(album);
        // console.log(value.data);
        if(value.data.message=="成功"){
            axios({
                method:'put',
                url:'/Sadmin/User/putAdmin',
                headers: {
                    'Content-Type':'application/x-www-form-urlencoded',
                    token:(req.session.token?req.session.token:'')
                },
                params:{
                    id:id,
                    account:account||accountv,
                    age:age||agev,
                    album:album||albumv,
                    area:area||areav,
                    email:email||emailv,
                    fans:fans||fansv,
                    name:names||namev,
                    password:password||"",
                    portrait:portrait||portraitv,
                    resume:resume||resumev,
                    sex:sex||sexv,
                    multipartFile:"D:\前端\background\me.jpg"
                }
            }).then(data=>{
                // console.log(data.data);
                console.log("正确");
                res.status(200).json({
                    key:data.data
                })
            }).catch(error=>{
                console.log("错误");
                // console.log(error);
                res.status(200).json({
                    error:"错误"
                })
            })
        }
    }).catch(err=>{
        console.log("错误1");
        res.status(200).json({
            error:"错误1"
        })
    })
})



module.exports=admin